package com.gitee.cashzhang27.test.boot.reactive.webflux.handler;

import com.gitee.cashzhang27.test.boot.reactive.webflux.dao.CityMongoRepository;
import com.gitee.cashzhang27.test.boot.reactive.webflux.entity.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @author Cash Zhang
 * @version v1.0
 * @since 2019/03/28 17:00
 */
@Component
public class CityMongoHandler {

  private final CityMongoRepository cityRepository;

  @Autowired
  public CityMongoHandler(CityMongoRepository cityRepository) {
    this.cityRepository = cityRepository;
  }

  public Mono<City> save(City city) {
    return cityRepository.save(city);
  }

  public Mono<City> findCityById(Long id) {

    return cityRepository.findById(id);
  }

  public Flux<City> findAllCity() {

    return cityRepository.findAll();
  }

  public Mono<City> modifyCity(City city) {

    return cityRepository.save(city);
  }

  public Mono<Long> deleteCity(Long id) {
    cityRepository.deleteById(id);
    return Mono.create(cityMonoSink -> cityMonoSink.success(id));
  }
}
